package edu.uwlax.cs.oayonlinestore.client.gui.admin;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.utils.DefaultAsyncCallback;
import edu.uwlax.cs.oayonlinestore.client.utils.DekoTable;
import edu.uwlax.cs.oayonlinestore.client.utils.TitlePage;
import edu.uwlax.cs.oayonlinestore.vo.UserVO;

public class PageAdminIndustrialCustomer extends TitlePage {

	private class ApproveTable extends DekoTable {
		Button approve;

		public ApproveTable() {
			super();
			addTitle("First Name");
			addTitle("Last Name");
			addTitle("");
		}

		public void addItem(UserVO u) {
			AddRow r = addRow();
			r.add(u.getFirstname());
			r.add(u.getLastname());
			r.add(approve = new Button("Approve"));
			approve.addClickListener(new ButtonClickListener(u));
		}

		public class ButtonClickListener implements ClickListener {
			UserVO vo = null;

			public ButtonClickListener(UserVO u) {
				vo = u;
			}

			public void onClick(Widget arg0) {
				OnlinestoreRService.Util.getInstance().approveIndustrialUser(
						vo.getId(), new DoneAddAsycHandler());
			}
		}

		public class DoneAddAsycHandler extends DefaultAsyncCallback {

			public void onSuccess(Object o) {

				Window.alert("User has been approved");
				onShow();
			}
		}

	}

	ApproveTable t;

	protected void create() {
		setTitle("Approve Industrial Customers");
		add(t = new ApproveTable());
	}

	protected void onShow() {

		OnlinestoreRService.Util.getInstance().getApprovableIndustrialUsers(
				getAsyncHandler());
	}

	protected void onDataArrived(Object o) {

		t.cleanExceptHeader();
		UserVO[] users = (UserVO[]) o;
		for (int i = 0; i < users.length; i++)
			t.addItem(users[i]);
	}

	public String getID() {
		return "AdminIndustrialUsers";
	}

}
